package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateTaskManager
//@description: 创建TaskManager记录
//@param: taskManager model.TaskManager
//@return: err error

func CreateTaskManager(taskManager model.TaskManager) (err error) {
	err = global.GVA_DB.Create(&taskManager).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteTaskManager
//@description: 删除TaskManager记录
//@param: taskManager model.TaskManager
//@return: err error

func DeleteTaskManager(taskManager model.TaskManager) (err error) {
	err = global.GVA_DB.Delete(&taskManager).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteTaskManagerByIds
//@description: 批量删除TaskManager记录
//@param: ids request.IdsReq
//@return: err error

func DeleteTaskManagerByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.TaskManager{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateTaskManager
//@description: 更新TaskManager记录
//@param: taskManager *model.TaskManager
//@return: err error

func UpdateTaskManager(taskManager model.TaskManager) (err error) {
	err = global.GVA_DB.Save(&taskManager).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetTaskManager
//@description: 根据id获取TaskManager记录
//@param: id uint
//@return: err error, taskManager model.TaskManager

func GetTaskManager(id uint) (err error, taskManager model.TaskManager) {
	err = global.GVA_DB.Where("id = ?", id).First(&taskManager).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetTaskManagerInfoList
//@description: 分页获取TaskManager记录
//@param: info request.TaskManagerSearch
//@return: err error, list interface{}, total int64

func GetTaskManagerInfoList(info request.TaskManagerSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.TaskManager{})
    var taskManagers []model.TaskManager
    // 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&taskManagers).Error
	return err, taskManagers, total
}